/// Author: QP
/// Version: 1.2
/// Date:2023/03/27
/// 工具类
/// Rule,DT扩展函数集
Class HCC.Utils.Functions Extends Ens.Rule.FunctionSet
{

/// 将UTC时间戳转为本地时间戳
/// Ensemble消息处理时间以UTC时间戳保存
ClassMethod UTCToLocal(pTS As %TimeStamp) As %TimeStamp [ SqlProc ]
{
   Quit $zdt($System.Util.UTCtoLocalWithZTIMEZONE($zdth(pTS,3)),3)
}

ClassMethod toUTF8(ByRef tOutput As %GlobalCharacterStream)
{
	Set tLen=3600000, tStreamBuffer="",tTempString=""
	Do tOutput.Rewind()
	While tLen>0
		{
			Set tTempString= tOutput.Read(.tLen,.tSC) 
			Set tTempString= $ZCVT(tTempString,"I","UTF8",handler)
			set tTempString=$REPLACE(tTempString,"&lt;","<")
			set tTempString=$REPLACE(tTempString,"&gt;",">")
			Set tStreamBuffer = tStreamBuffer_tTempString
		}
		Do tOutput.Clear()
		Do tOutput.Write(tStreamBuffer)
}

ClassMethod toUTF8Input(ByRef tOutput As %GlobalCharacterStream)
{
	Set tLen=3600000, tStreamBuffer="",tTempString=""
	Do tOutput.Rewind()
	While tLen>0
		{
			Set tTempString= tOutput.Read(.tLen,.tSC) 
			Set tTempString= $ZCVT(tTempString,"I","UTF8",handler)
			set tTempString=$REPLACE(tTempString,"&lt;","<")
			set tTempString=$REPLACE(tTempString,"&gt;",">")
			Set tStreamBuffer = tStreamBuffer_tTempString
		}
		Do tOutput.Clear()
		Do tOutput.Write(tStreamBuffer)
}

/// 用XPath获得XML中的数据
/// pStr: XML字符串
/// pXPath: xpath表达式
/// pXMLNS: 使用的XML NS
ClassMethod GetDataByXPath(pStr = "<item use=""IDE"" xmlns=""urn:hl7-org:v3""><part value=""test"" xmlns=""urn:hl7-org:v3""/></item>", pXPath = "//s:item/s:part/@value", pXMLNS = "urn:hl7-org:v3")
{
    //Set pStr="<item xmlns='urn:hl7-org:v3'><part value='张三' xmlns='urn:hl7-org:v3'/></item>	"
    // Create an XPATH Document instance from the stream of XML
    s tXMLStrUTF8=$zcvt(pStr,"O","UTF8")
    Set tSC=##class(%XML.XPATH.Document).CreateFromString(tXMLStrUTF8,.tDocument)
    If $$$ISERR(tSC) Quit ""

	Set:(pXMLNS'="") tDocument.PrefixMappings="s "_pXMLNS                      
	Set tSC	= tDocument.EvaluateExpression(pXPath, ".", .tResults)
	Quit tResults.GetAt(1).Value
}

}
